package com.xinsmartsky.penmaji.penmaji;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

public class LoginActivity2 extends Activity implements View.OnClickListener {
    private View login2_line_root;
    private PopupWindow popupWindow;
    private Button login2_btn_edit;
    private Button login2_btn_print;
    private int devfd = -1;
    private SerialPortActivity serialport = new SerialPortActivity();
    private String commend = null;//供墨状态
    private TextView login2_tv_text0;
    private TextView login2_tv_text1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login2);
        devfd = getIntent().getIntExtra("fd", 0);//默认值为0
        commend = getIntent().getStringExtra("commend");
        findViewById();
        setOnClickListener();
        initPopupWindow(0);
        show();
        Runner1 r = new Runner1();
        Thread t = new Thread(r);
        t.start();
    }

    class Runner1 implements Runnable {

        @Override
        public void run() {
            if (commend.equals("shangmo")) {
                serialport.CommuniProcess(devfd, (byte) 0xf6);
            }
//            if () {//上墨成功
//                initPopupWindow(1);
//            } else {//上墨失败
//                initPopupWindow(2);
//            }
        }
    }

    public void show() {
        login2_line_root.post(new Runnable() {
            @Override
            public void run() {
                if (popupWindow != null) {
                    popupWindow.showAtLocation(login2_line_root, Gravity.BOTTOM, 0, 0);
                }

            }
        });
    }


    private void findViewById() {
        login2_btn_edit = (Button) findViewById(R.id.login2_btn_edit);
        login2_line_root = findViewById(R.id.login2_line_root);
        login2_btn_print = (Button) findViewById(R.id.login2_btn_print);
        login2_tv_text0 = (TextView) findViewById(R.id.login2_tv_text0);
        login2_tv_text1 = (TextView) findViewById(R.id.login2_tv_text1);
    }

    private void setOnClickListener() {
        login2_btn_edit.setOnClickListener(this);
        login2_btn_print.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        switch (v.getId()) {
            case R.id.login2_btn_edit: {
                bundle.putInt("login2editfd", devfd);
                intent.putExtras(bundle);
                intent.setClass(LoginActivity2.this, EditActivity.class);
                startActivity(intent);
                break;
            }
            case R.id.login2_btn_print: {
                bundle.putInt("login2printfd", devfd);
                intent.putExtras(bundle);
                intent.setClass(LoginActivity2.this, PrintSetActivity.class);
                startActivity(intent);
                break;
            }
        }
    }

    private void initPopupWindow(final int num) {
        View contentView = getLayoutInflater().inflate(R.layout.activity_login2_menu, null);
        final Button login2_btn_yes = (Button) contentView.findViewById(R.id.login2_btn_yes);
        final Button login2_btn_no = (Button) contentView.findViewById(R.id.login2_btn_no);
        final TextView login2_tv_errornum = (TextView) contentView.findViewById(R.id.login2_tv_errornum);
        final TextView login2_tv_fail = (TextView) contentView.findViewById(R.id.login2_tv_fail);
        final TextView login2_tv_wait = (TextView) contentView.findViewById(R.id.login2_tv_wait);
        final LinearLayout login2_line_01 = (LinearLayout) contentView.findViewById(R.id.login2_line_01);
        LinearLayout login2_line_02 = (LinearLayout) contentView.findViewById(R.id.login2_line_02);
        login2_btn_yes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (num) {
                    case 1: {//是否断墨清洗中的“是”
                        serialport.CommuniProcess(devfd, (byte) 0xf2);
                        break;
                    }
                    case 3: {//是否继续向喷头供应墨水中的“是”
                        //指向上墨键
                        serialport.CommuniProcess(devfd, (byte) 0xf6);
//                        if () {//上墨成功
//
//                        } else {//上墨失败
//
//                        }
                        break;
                    }
                }
            }
        });
        login2_btn_no.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (popupWindow.isShowing()) {
                    popupWindow.dismiss();
                }
            }
        });
        login2_tv_fail.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                //解除警报 （按两次返回键）
                serialport.CommuniProcess(devfd, (byte) 0x82);
                //疏通
                login2_tv_fail.setText("向喷头供应墨水失败  正在进行疏通操作");
                login2_tv_wait.setText("请等待......");
                login2_tv_wait.setVisibility(0);
                //需要设置3个才能禁止点击事件
                login2_tv_fail.setClickable(false);
                login2_tv_fail.setFocusable(false);
                login2_tv_fail.setFocusableInTouchMode(false);

//                if () {//已经解除警报
//                    //疏通
//                    serialport.CommuniProcess(devfd, (byte) 0xec);
//                    if () {//如果疏通完成
//                        login2_tv_errornum.setVisibility(8);
//                        login2_tv_fail.setVisibility(8);
//                        login2_tv_wait.setText("疏通操作完成 是否继续向喷头供应墨水？");
//                        num = 3;
//                        login2_line_01.setVisibility(0);
//                    }
//                }
            }
        });
        switch (num) {
            case 0: {
                login2_tv_fail.setText("正在为喷头供应墨水...");
                login2_tv_fail.setFocusable(false);
                login2_tv_fail.setVisibility(0);
                break;
            }
            case 1: {
                login2_tv_text0.setVisibility(0);
                login2_tv_text1.setVisibility(0);
                login2_tv_wait.setText("是否断墨清洗？");
                login2_tv_wait.setVisibility(0);
                login2_line_01.setVisibility(0);
                login2_line_02.setVisibility(0);
                //显示系统信息  乙机？？？？？？？
                break;
            }
            case 2: {
                login2_tv_errornum.setText("故障编号  17");
                login2_tv_fail.setVisibility(0);
                login2_tv_fail.setText("点击此处解除警报");
                login2_tv_fail.setFocusable(true);
                login2_tv_fail.setFocusableInTouchMode(true);
                break;
            }
        }
        popupWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setOutsideTouchable(false);
        popupWindow.setFocusable(true);
        popupWindow.setAnimationStyle(R.style.popupanimation);
    }

}
